package com.dou.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dou.yygh.common.exception.YyghException;
import com.dou.yygh.common.result.Result;
import com.dou.yygh.hosp.mapper.HospitalSetMapper;
import com.dou.yygh.hosp.service.HospitalSetService;
import com.dou.yygh.hosp.util.MD5;
import com.dou.yygh.model.hosp.HospitalSet;
import com.dou.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/08/2021/8/20/23:41
 * @Description:
 */
@Api( tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    //1查询医院设置表所有信息
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAllHospital(){
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        List<HospitalSet> list = hospitalSetService.list(wrapper);
        return Result.ok(list);
    }


    //删除数据
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("/{id}")
    public Result removeHospitalSet(@PathVariable long id){
        boolean b = hospitalSetService.removeById(id);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    //条件分页查询
@PostMapping("/findPage/{current}/{limit}")
    public Result  findHospitalSet(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
//创建page对象
    Page<HospitalSet> page = new Page<>(current, limit);
  String hosname=  hospitalSetQueryVo.getHoscode();//医院的名称
  String hoscode=  hospitalSetQueryVo.getHoscode();//医院的编号
    //构造条件
    QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
    if (!StringUtils.isEmpty(hosname)){
        queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
    }
    if(!StringUtils.isEmpty(hoscode)){
        queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
    }
    //调用方法实现分页查询
    Page<HospitalSet> page1 = hospitalSetService.page(page, queryWrapper);
    return Result.ok(page1);
        }


        //添加医院设置
    @PostMapping("/saveHospitalSet")
    public Result addHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态
        hospitalSet.setStatus(1);
        //签名密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
         if (save){
             return Result.ok();
         }else
         {
             return Result.fail();
         }
    }

    //根据id获取医院
    @CrossOrigin
    @PostMapping("/getHospitalSet/{id}")
    public Result getHospitalSet(@PathVariable long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }
    //修改医院设置
    @PostMapping("/updatHospitalSet")
    public Result updatHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b? Result.ok():Result.fail();
    }
    //批量删除医院
    @DeleteMapping("/batchHospitalSet")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        return b? Result.ok():Result.fail();
    }
    //医院设置的锁定和解锁
    @CrossOrigin
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        //根据id查询医院信息
        HospitalSet byId = hospitalSetService.getById(id);
        //设置状态
        byId.setStatus(status);
        boolean b = hospitalSetService.updateById(byId);
        return b? Result.ok():Result.fail();
    }
    //发送签名的密钥
    @PutMapping("/sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id){
        //根据id查询医院信息
        HospitalSet byId = hospitalSetService.getById(id);
        //设置状态
        String signKey = byId.getSignKey();
        String hoscode = byId.getHoscode();

        //ToDo发送短信
        return Result.ok();
    }
}
